package tum0r.image;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
 * 图片水印类，可以给图片打上文字水印和图片水印
 * <p>
 * ClassName: Watermark
 * Author: tum0r
 * Time: 2020/6/12 10:20
 */
public class Watermark extends ImageBase {
    // 是否打文字水印
    private boolean textWatermark = true;
    // 水印文字内容
    private String text = null;
    // 文字字体
    private Font font = new Font("宋体", Font.PLAIN, 15);
    // 文字颜色
    private Color color = new Color(232, 232, 232, 150);

    // 是否打图片水印
    private boolean imageWatermark = false;
    // 水印图片
    private BufferedImage image = null;
    // 图片透明度
    private int alpha = 150;
    // 图片宽度
    private int imageWidth = 15;
    // 图片高度
    private int imageHeight = 15;

    // 水印位置
    private Point point = new Point(0, 0);


    public boolean isTextWatermark() {
        return textWatermark;
    }

    public void setTextWatermark(boolean textWatermark) {
        this.textWatermark = textWatermark;
    }

    public boolean isImageWatermark() {
        return imageWatermark;
    }

    public void setImageWatermark(boolean imageWatermark) {
        this.imageWatermark = imageWatermark;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public Font getFont() {
        return font;
    }

    public void setFont(Font font) {
        if (font != null) {
            this.font = font;
        }
    }

    public BufferedImage getImage() {
        return image;
    }

    public void setImage(String path) throws IOException {
        setImage(new File(path));
    }

    public void setImage(File imageFile) throws IOException {
        setImage(ImageIO.read(imageFile));
    }

    public void setImage(BufferedImage image) {
        this.image = image;
    }

    public int getImageWidth() {
        return imageWidth;
    }

    public void setImageWidth(int imageWidth) {
        if (imageWidth >= 0) {
            this.imageWidth = imageWidth;
        }
    }

    public void setImageSize(int imageWidth, int imageHeight) {
        setImageWidth(imageWidth);
        setImageHeight(imageHeight);
    }

    public int getImageHeight() {
        return imageHeight;
    }

    public void setImageHeight(int imageHeight) {
        if (imageHeight >= 0) {
            this.imageHeight = imageHeight;
        }
    }

    public Point getPoint() {
        return point;
    }

    public void setPoint(int x, int y) {
        setPoint(new Point(x, y));
    }

    public void setPoint(Point point) {
        if (point != null) {
            this.point = point;
        }
    }

    public int getAlpha() {
        return alpha;
    }

    public void setAlpha(int alpha) {
        if (alpha >= 0 && alpha <= 255) {
            this.alpha = alpha;
        }
    }

    public Color getColor() {
        return color;
    }

    public void setColor(Color color) {
        if (color != null) {
            this.color = color;
        }
    }

    /**
     * 处理图片，打上水印
     * textWatermark的值打文字水印，水印内容通过 setText 传值过来
     * imageWatermark的值打图片水印，水印内容通过 setImage 传递过来
     *
     * @param image 要打水印的图片BufferedImage对象
     * @return 打完水印后图片BufferedImage对象
     */
    @Override
    public BufferedImage handle(BufferedImage image) {
        BufferedImage result = null;
        if (image != null) {
            int width = image.getWidth();
            int height = image.getHeight();
            result = image.getSubimage(0, 0, width, height);
            Graphics2D graphics = (Graphics2D) result.getGraphics();
            if (textWatermark && text != null) {
                graphics.setFont(font);
                graphics.setColor(color);
                graphics.drawString(text, (float) point.getX(), (float) point.getY() + font.getSize2D() * 5 / 6);
            }
            if (imageWatermark && this.image != null) {
                for (int x = 0; x < this.image.getWidth(); x++) {
                    for (int y = 0; y < this.image.getHeight(); y++) {
                        int RGB = this.image.getRGB(x, y);
                        int red = (RGB >> 16) & 0xff;
                        int green = (RGB >> 8) & 0xff;
                        int blue = RGB & 0xff;
                        this.image.setRGB(x, y, new Color(red, green, blue, alpha).getRGB());
                    }
                }
                graphics.drawImage(this.image, (int) point.getX(), (int) point.getY(), imageWidth, imageHeight + (int) (font.getSize2D() * 5 / 6), null);
            }
        }
        return result;
    }
}
